<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 测试controller 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
pf_load::include_lib('pf_upload');
class attach_action extends pf_controller{
	protected $category_obj = null;

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		die("upload");
	}


	public function upload()
	{
		//文件保存目录路径
		$save_path = './attached/';
		//文件保存目录URL
		$save_url = './attached/';
		//定义允许上传的文件扩展名
		$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
		//最大文件大小
		$max_size = 2;
		$upload = new pf_upload($max_size);

		$upload->set_upload_type($ext_arr);//设置上传文件类型
		$upload->set_upload_path($save_path);//路径
		//有上传文件时
		if (empty($_FILES) === false) {
			$upload_info = $upload->upload($_FILES['imgFile'], 'images', 0,$f_r_id =0);//上传
			//判断
			if (false === $upload_info) {
				$error_info = $upload->get_error_info();
				switch($error_info['code']) {
					case '107000':
						$uploadStat = '此类型不允许上传！';
						break;
					case '107001':
						$uploadStat = '文件大小超过了2M！';
						break;
					default:
						$uploadStat = '系统错误， 不允许上传！';
						break;
				}

				alert($uploadStat);
			} else {
				$file_url = $save_url . $upload_info['f_filename'];
				header('Content-type: text/html; charset=UTF-8');
				echo json_encode(array('error' => 0, 'url' => $file_url));
				exit;
			}
		}
	
	}

}

function alert($msg) {
	header('Content-type: text/html; charset=UTF-8');
	echo json_encode(array('error' => 1, 'message' => $msg));
	exit;
}
